var city = "";
var days = -1;//-1表示不限制，0表示小于3天，1表示3到7天，2表示大于7天
var starttime = "";
var endtime = "";
var startprice = 0;
var endprice = 0;
var selecttype = 2;//2表示不限制，0表示按销量，1表示按价格
var order = 0;//1表示升序，0表示降序
var page = 1;//表示第几页
$(document).ready(function(){
	loadP();
	WantThreeOrAllMonth();
	chooseD();
	
	ChoosePrice();
	NormalChoose();
	
	SearchAll();
	wantAllLine(city,days,starttime,endtime,startprice,endprice,selecttype,order,page);
	
});
//重置页面数据
function loadP(){
	city = $("input.linelist_search_item_input").val();
	$("div.linelist_center_left_choose_day_one:eq(1)").addClass("yellowt");
	$("div.linelist_center_left_choose_start_one:eq(1)").addClass("yellowt");
	$("div.linelist_center_left_choose_price_one:eq(1)").addClass("yellowt");
}
//选择行程天数
function chooseD(){
	$("div.linelist_center_left_choose_day_one:eq(1)").click(function(){
		$(this).addClass("yellowt");
		$("div.linelist_center_left_choose_day>div").not(this).removeClass("yellowt");		
		$("div.linelist_center_left_choose_you_day").fadeOut();
		days = -1;
		wantAllLine(city,days,starttime,endtime,startprice,endprice,selecttype,order,page);
		console.log("days="+days);
	});
	$("div.linelist_center_left_choose_day_one:eq(2)").click(function(){
		$(this).addClass("yellowt");
		$("div.linelist_center_left_choose_day>div").not(this).removeClass("yellowt");		
		$("div.linelist_center_left_choose_you_day").fadeIn();
		$("div.linelist_center_left_choose_you_day_two").text("3天以下");
		days = 0;
		wantAllLine(city,days,starttime,endtime,startprice,endprice,selecttype,order,page);
		console.log("days="+days);
	});
	$("div.linelist_center_left_choose_day_one:eq(3)").click(function(){
		$(this).addClass("yellowt");
		$("div.linelist_center_left_choose_day>div").not(this).removeClass("yellowt");		
		$("div.linelist_center_left_choose_you_day").fadeIn();
		$("div.linelist_center_left_choose_you_day_two").text("3至7天");
		days = 1;
		wantAllLine(city,days,starttime,endtime,startprice,endprice,selecttype,order,page);
		console.log("days="+days);
	});
	$("div.linelist_center_left_choose_day_one:eq(4)").click(function(){
		$(this).addClass("yellowt");
		$("div.linelist_center_left_choose_day>div").not(this).removeClass("yellowt");		
		$("div.linelist_center_left_choose_you_day").fadeIn();
		$("div.linelist_center_left_choose_you_day_two").text("7天以上");
		days = 2;
		wantAllLine(city,days,starttime,endtime,startprice,endprice,selecttype,order,page);
		console.log("days="+days);
	});
	
}
//选择2个时间内最大时间,即最晚时间
function MaxTime(t1,t2){
	var year1 = parseInt(t1.split("-")[0]);
	var month1 = parseInt(t1.split("-")[1]);
	var year2 = parseInt(t2.split("-")[0]);
	var month2 = parseInt(t2.split("-")[1]);
	if(year1 > year2){
		return t1;
	}else if(year1 == year2){
		if(month1 > month2){
			return t1;
		}else if(month1 == month2){
			return t1;
		}else{
			return t2;
		}
	}else{
		return t2;
	}
	
}
//选择出发时时间
function ChooseTime(){
	$("div.linelist_center_left_choose_start_one:eq(1)").click(function(){
		$(this).addClass("yellowt");
		$("div.linelist_center_left_choose_start_onethreemonth>div.linelist_center_left_choose_start_one").removeClass("yellowt");
		starttime="";
		endtime="";
		$("div.linelist_center_left_choose_you_start").fadeOut();
		wantAllLine(city,days,starttime,endtime,startprice,endprice,selecttype,order,page);
	});
	for(var i=2;i<5;i++){
		$("div.linelist_center_left_choose_start_one:eq("+i+")").click(function(i){
			return function(){
				$(this).addClass("yellowt");
				$("div.linelist_center_left_choose_start_onethreemonth>div").not(this).removeClass("yellowt");
				var m = $("div.linelist_center_left_choose_start_one:eq("+i+")").text();
				starttime = m;
				endtime = m;
				$("div.linelist_center_left_choose_you_start_two:eq(0)").text(m);
				$("div.linelist_center_left_choose_you_start_two:eq(2)").text(m);
				$("div.linelist_center_left_choose_you_start").fadeIn();
				wantAllLine(city,days,starttime,endtime,startprice,endprice,selecttype,order,page);
			}
		}(i))
	}
	$("input.linelist_center_left_choose_start_yes").click(function(){
		var startt = $("select.linelist_center_left_choose_start_two_s:eq(0)").val();
		var endt = $("select.linelist_center_left_choose_start_two_s:eq(1)").val();
		endtime = MaxTime(startt,endt);
		starttime = (endtime == startt?endt:startt);
		$("div.linelist_center_left_choose_you_start_two:eq(0)").text(starttime);
		$("div.linelist_center_left_choose_you_start_two:eq(2)").text(endtime);
		$("div.linelist_center_left_choose_start_onethreemonth>div").removeClass("yellowt");
		$("div.linelist_center_left_choose_you_start").fadeIn();
		wantAllLine(city,days,starttime,endtime,startprice,endprice,selecttype,order,page);
		console.log("starttime="+starttime);
		console.log("endtime="+endtime);
	});
	$("input.linelist_center_left_choose_start_no").click(function(){
		$("div.linelist_center_left_choose_start_onethreemonth>div").removeClass("yellowt");
		$("div.linelist_center_left_choose_start_one:eq(1)").addClass("yellowt");		
		starttime = "";
		endtime = "";
		$("div.linelist_center_left_choose_you_start").fadeOut();
		wantAllLine(city,days,starttime,endtime,startprice,endprice,selecttype,order,page);
	});
}
//选择价格区间
function ChoosePrice(){
	$("div.linelist_center_left_choose_price_one:eq(1)").click(function(){
		$(this).addClass("yellowt");
		$("div.linelist_center_left_choose_price>div").not(this).removeClass("yellowt");
		startprice = 0;
		endprice = 0;
		$("div.linelist_center_left_choose_you_price").fadeOut();
		wantAllLine(city,days,starttime,endtime,startprice,endprice,selecttype,order,page);
	});
	$("div.linelist_center_left_choose_price_one:eq(2)").click(function(){
		$(this).addClass("yellowt");
		$("div.linelist_center_left_choose_price>div").not(this).removeClass("yellowt");
		startprice = 0;
		endprice = 1000;
		$("div.linelist_center_left_choose_you_price_two:eq(1)").text("0");
		$("div.linelist_center_left_choose_you_price_two:eq(2)").text("至");
		$("div.linelist_center_left_choose_you_price_two:eq(3)").text("1000");
		$("div.linelist_center_left_choose_you_price").fadeIn();
		wantAllLine(city,days,starttime,endtime,startprice,endprice,selecttype,order,page);
	});
	$("div.linelist_center_left_choose_price_one:eq(3)").click(function(){
		$(this).addClass("yellowt");
		$("div.linelist_center_left_choose_price>div").not(this).removeClass("yellowt");
		startprice = 1001;
		endprice = 5000;
		$("div.linelist_center_left_choose_you_price_two:eq(1)").text("1001");
		$("div.linelist_center_left_choose_you_price_two:eq(2)").text("至");
		$("div.linelist_center_left_choose_you_price_two:eq(3)").text("5000");
		$("div.linelist_center_left_choose_you_price").fadeIn();
		wantAllLine(city,days,starttime,endtime,startprice,endprice,selecttype,order,page);
	});
	$("div.linelist_center_left_choose_price_one:eq(4)").click(function(){
		$(this).addClass("yellowt");
		$("div.linelist_center_left_choose_price>div").not(this).removeClass("yellowt");
		startprice = 5001;
		endprice = 999999;
		$("div.linelist_center_left_choose_you_price_two:eq(1)").text("5000以上");
		$("div.linelist_center_left_choose_you_price_two:eq(2)").text("");
		$("div.linelist_center_left_choose_you_price_two:eq(3)").text("");
		$("div.linelist_center_left_choose_you_price").fadeIn();
		wantAllLine(city,days,starttime,endtime,startprice,endprice,selecttype,order,page);
	});
	$("input.linelist_center_left_choose_price_yes").click(function(){
		var p1 = $("input.linelist_center_left_choose_price_two_t:eq(0)").val();
		var p2 = $("input.linelist_center_left_choose_price_two_t:eq(1)").val();
		var sp1 = parseInt(p1);
		var sp2 = parseInt(p2);
		if(p1 != '' && p2 != ''){
			sp1 = (sp1>0?sp1:0);
			sp2 = (sp2>0?sp2:0);
			startprice = (sp1 > sp2?sp2:sp1);
			endprice = (sp1 > sp2?sp1:sp2);
			$("div.linelist_center_left_choose_price_one>div").removeClass("yellowt");
			$("div.linelist_center_left_choose_you_price_two:eq(1)").text(startprice);
			$("div.linelist_center_left_choose_you_price_two:eq(2)").text("至");
			$("div.linelist_center_left_choose_you_price_two:eq(3)").text(endprice);
			$("div.linelist_center_left_choose_you_price").fadeIn();
			wantAllLine(city,days,starttime,endtime,startprice,endprice,selecttype,order,page);
			console.log("startprice="+startprice);
			console.log("endprice="+endprice);
		}else if( p1 != '' && p2 == ''){
			startprice = (sp1>0?sp1:0) ;
			endprice = 999999;
			$("div.linelist_center_left_choose_price_one>div").removeClass("yellowt");
			$("div.linelist_center_left_choose_you_price_two:eq(1)").text(startprice);
			$("div.linelist_center_left_choose_you_price_two:eq(2)").text("至");
			$("div.linelist_center_left_choose_you_price_two:eq(3)").text("999+");
			$("div.linelist_center_left_choose_you_price").fadeIn();
			wantAllLine(city,days,starttime,endtime,startprice,endprice,selecttype,order,page);
		}else if( p1 == '' && p2 != ''){
			startprice = 0;
			endprice = (sp2>0?sp2:999999);
			$("div.linelist_center_left_choose_price_one>div").removeClass("yellowt");
			$("div.linelist_center_left_choose_you_price_two:eq(1)").text(startprice);
			$("div.linelist_center_left_choose_you_price_two:eq(2)").text("至");
			$("div.linelist_center_left_choose_you_price_two:eq(3)").text(sp2>0?sp2:'999+');
			$("div.linelist_center_left_choose_you_price").fadeIn();
			wantAllLine(city,days,starttime,endtime,startprice,endprice,selecttype,order,page);
		}
	});
	$("input.linelist_center_left_choose_price_no").click(function(){
		startprice = 0;
		endprice = 0;
		$("div.linelist_center_left_choose_price>div").removeClass("yellowt");
		$("div.linelist_center_left_choose_price_one:eq(1)").addClass("yellowt");
		$("div.linelist_center_left_choose_you_price").fadeOut();
		wantAllLine(city,days,starttime,endtime,startprice,endprice,selecttype,order,page);
	});
}
//综合排序及销量及价格绑定事件
function NormalChoose(){
	$("div.linelist_center_left_order_item:eq(0)").click(function(){
		order = (order == 0?1:0);
		selecttype = 2;
		wantAllLine(city,days,starttime,endtime,startprice,endprice,selecttype,order,page);
		$(this).addClass("linelist_center_left_order_itemchoose");
		$("div.linelist_center_left_order>div").not(this).removeClass("linelist_center_left_order_itemchoose");
	});
	$("div.linelist_center_left_order_item:eq(1)").click(function(){
		order = (order == 0?1:0);
		selecttype = 0;
		wantAllLine(city,days,starttime,endtime,startprice,endprice,selecttype,order,page);
		$(this).addClass("linelist_center_left_order_itemchoose");
		$("div.linelist_center_left_order>div").not(this).removeClass("linelist_center_left_order_itemchoose");
	});
	$("div.linelist_center_left_order_item:eq(2)").click(function(){
		order = (order == 0?1:0);
		selecttype = 1;
		wantAllLine(city,days,starttime,endtime,startprice,endprice,selecttype,order,page);
		$(this).addClass("linelist_center_left_order_itemchoose");
		$("div.linelist_center_left_order>div").not(this).removeClass("linelist_center_left_order_itemchoose");
	});
}
//您已选择取消事件
function CancelChoose(){
	$("div.linelist_center_left_choose_you_day_three").click(function(){
		$("div.linelist_center_left_choose_you_day").fadeOut();
		days = -1;
		$("div.linelist_center_left_choose_day>div").removeClass("yellowt");
		$("div.linelist_center_left_choose_day_one:eq(1)").addClass("yellowt");
		wantAllLine(city,days,starttime,endtime,startprice,endprice,selecttype,order,page);
	});
	$("div.linelist_center_left_choose_you_start_three").click(function(){
		$("div.linelist_center_left_choose_you_start").fadeOut();
		var starttime = "";
		var endtime = "";
		$("div.linelist_center_left_choose_start_onethreemonth>div").removeClass("yellowt");
		$("div.linelist_center_left_choose_start_one:eq(1)").addClass("yellowt");
		wantAllLine(city,days,starttime,endtime,startprice,endprice,selecttype,order,page);
	});
	$("div.linelist_center_left_choose_you_price_three").click(function(){
		$("div.linelist_center_left_choose_you_price").fadeOut();
		var startprice = 0;
		var endprice = 0;
		$("div.linelist_center_left_choose_price>div").removeClass("yellowt");
		$("div.linelist_center_left_choose_price_one:eq(1)").addClass("yellowt");
		wantAllLine(city,days,starttime,endtime,startprice,endprice,selecttype,order,page);
	});
}
//绑定搜索接口
function SearchAll(){
	$("input#SearchList").click(function(){
		wantAllLine(city,days,starttime,endtime,startprice,endprice,selecttype,order,page);
		console.log("开始查找");
	});
}
//绑定选择页数事件
function ChoosePage(){
	var t = $("div.linelist_center_left_page_center_item");
	for(var i =0;i<t.length;i++){
		$("div.linelist_center_left_page_center_item:eq("+i+")").click(function(i){
			return function(){
				page = $("div.linelist_center_left_page_center_item:eq("+i+")").text();
				wantAllLine(city,days,starttime,endtime,startprice,endprice,selecttype,order,page);
			}
		}(i))
	}
}
//跳转线路详情
function GotoLineinfo(){
	var l = $("div.linelist_center_left_pro").length;
	for(var i =0;i<l;i++){
		$("div.linelist_center_left_pro:eq("+i+")").click(function(i){
			return function(){
				var lineid = $("div.linelist_center_left_production>font:eq("+i+")").text();
				var linecity = city;
				var url = basePath+"/lineinfo?lineid="+lineid+"&linecity="+linecity;
				url = encodeURI(url);
				url = encodeURI(url);
				window.location.href = (url);	
			}
		}(i))
	}
}
//请求线路数据
function wantAllLine(city,days,starttime,endtime,startprice,endprice,selecttype,order,page){
	LoadingIcon = layer.load();
	city = $("input.linelist_search_item_input").val();
	var imgbx=basePath+"/static/pcsofangche/line/linelist/img/";
	console.log("city="+city);
	console.log("days="+days);
	console.log("starttime="+starttime);
	console.log("endtime="+endtime);
	console.log("startprice="+startprice);
	console.log("endprice="+endprice);
	console.log("selecttype="+selecttype);
	console.log("order="+order);
	console.log("page="+page);
	var imgb=basePath+"/static/pcsofangche/line/lineindex/img/";
	//格式化内容
	$("div.linelist_center_left_production").html("");
	$("div.linelist_center_left_page_center").html("");
	//设置title
	$(document).attr("title",city+"线路搜索");//修改title值  
	$.ajax({
		dataType : "json",
		type : "post",
		url : basePath+"/line/searchlineinfo",
		data : {
			city:city,
			days:days,
			starttime:starttime,
			endtime:endtime,
			startprice:startprice,
			endprice:endprice,
			selecttype:selecttype,
			order:order,
			page:page
		},
		
		contentType : "application/x-www-form-urlencoded;charset=utf-8",
		success : function(data) {
			var p = "";
			if(data.data == ""){
				layer.msg("找不到您想要的信息，试试其他条件搜索吧！");
				p = p + "<div class=\"linelist_center_left_proimg\"><img src=\""+imgbx+"listerror.png"+"\" class=\"linelist_center_left_proimg_posi\" /></div>"
				$("div.linelist_center_left_production").html(p);
			}else{
				for(var i=0;i<data.data.length;i++){
					var d = "";
					var f = "";
					for(var x=0;x<data.data[i].idea.length;x++){
						d = d + "<div class=\"linelist_center_left_pro_left_hot_item\">"+data.data[i].idea[x].name+"</div>\n";
					};
					for(var y=0;y<data.data[i].cha.length;y++){
						f = f + "<div class=\"linelist_center_left_pro_right_center_detail_fea_item\">"+data.data[i].cha[y].name+"</div>\n";
					};
					p = p + "<div class=\"linelist_center_left_pro row\">\n" +
					"<div class=\"col-lg-3 col-md-3 linelist_center_left_pro_left\">\n" +
					"<div class=\"linelist_center_left_pro_left_pic\">\n" +
					"<img src=\""+data.data[i].picture+"\" class=\"linelist_center_left_pro_left_pic_posi img-responsive\">\n" +
					"</div>\n" +
					"<div class=\"linelist_center_left_pro_left_hot clearfix\">\n" +
					d+
					"</div>\n" +
					"</div>\n" +
					"<div class=\"col-lg-9 col-md-9 linelist_center_left_pro_right\">\n" +
					"<div class=\"linelist_center_left_pro_right_title\">"+data.data[i].title+"</div>\n" +
					"<div class=\"linelist_center_left_pro_right_center row\">\n" +
					"<div class=\"col-lg-9 col-md-9 linelist_center_left_pro_right_center_detail\">\n" +
					"<div class=\"linelist_center_left_pro_right_center_detail_fea clearfix\">\n" +
					f+
					"</div>\n" +
					"<div class=\"linelist_center_left_pro_right_center_detail_path clearfix\">\n" +
					"<div class=\"linelist_center_left_pro_right_center_detail_path_one\">途径：</div>\n" +
					"<div class=\"linelist_center_left_pro_right_center_detail_path_two\">"+data.data[i].path+"</div>\n" +
					"</div>\n" +
					"<div class=\"linelist_center_left_pro_right_center_detail_more clearfix\">\n" +
					"<div class=\"linelist_center_left_pro_right_center_detail_more_city clearfix\">\n" +
					"<div class=\"linelist_center_left_pro_right_center_detail_more_city_pic\">\n" +
					"<img src=\""+imgb+"lineindex_city_small.png\" class=\"linelist_center_left_pro_right_center_detail_more_city_pic_posi\">\n" +
					"</div>\n" +
					"<div class=\"linelist_center_left_pro_right_center_detail_more_city_one\">出发地：</div>\n" +
					"<div class=\"linelist_center_left_pro_right_center_detail_more_city_two\">"+data.data[i].city+"</div>\n" +
					"</div>\n" +
					"<div class=\"linelist_center_left_pro_right_center_detail_more_day clearfix\">\n" +
					"<div class=\"linelist_center_left_pro_right_center_detail_more_day_pic\">\n" +
					"<img src=\"\" class=\"linelist_center_left_pro_right_center_detail_more_day_pic_posi\">\n" +
					"</div>\n" +
					"<div class=\"linelist_center_left_pro_right_center_detail_more_day_one\">行程天数：</div>\n" +
					"<div class=\"linelist_center_left_pro_right_center_detail_more_day_two\">"+data.data[i].days+"</div>\n" +
					"<div class=\"linelist_center_left_pro_right_center_detail_more_day_three\">天</div>\n" +
					"</div>\n" +
					"</div>\n" +
					"</div>\n" +
					"<div class=\"col-lg-3 col-md-3 linelist_center_left_pro_right_center_price\">\n" +
					"<div class=\"linelist_center_left_pro_right_center_price_price clearfix\">\n" +
					"<div class=\"linelist_center_left_pro_right_center_price_price_one\">起</div>\n" +
					"<div class=\"linelist_center_left_pro_right_center_price_price_two\">"+data.data[i].price+"</div>\n" +
					"<div class=\"linelist_center_left_pro_right_center_price_price_three\">￥</div>\n" +
					"</div>\n" +
					"<input type=\"button\" class=\"linelist_center_left_pro_right_center_price_b\" value=\"查看详情\">\n" +
					"</div>\n" +
					"</div>\n" +
					"</div>\n" +
					"</div>"+"<font class=\"hidden\">"+data.data[i].lineid+"</font>";
					
				};
				$("div.linelist_center_left_production").html(p);
				var tp = parseInt(data.data[0].totalpage);
				var ti = "";
				for(var z = 1;z <= tp;z++){
					ti = ti + "<div class=\"linelist_center_left_page_center_item shand\">"+z+"</div>"
				};
				$("div.linelist_center_left_page_center").html(ti);
				ChoosePage();
				GotoLineinfo();
			};
			
			
		},
		error : function() {
			console.log("访问失败");
		},
		complete : function() {
			console.log("请求完成");
			layer.close(LoadingIcon);
		},
	});
}
//页面加载时提供最近3个月的出发时间和最近一年的出发时间
function WantThreeOrAllMonth(){
	$.ajax({
		dataType : "json",
		type : "post",
		url : basePath+"/car/rentinfogivemonth",
		data : {

		},		
		contentType : "application/x-www-form-urlencoded;charset=utf-8",
		success : function(data) {
			var three = "";
			var all = "";
			for(var i = 0;i<3;i++){
				three = three + "<div class=\"linelist_center_left_choose_start_one shand\">"+data.data[i].month+"</div>";
			};
			for(var i = 0;i<data.data.length;i++){
				all = all + "<option value=\""+data.data[i].month+"\">"+data.data[i].month+"</option>"
			};
			$("div.linelist_center_left_choose_start_onethreemonth").html(three);
			$("select.linelist_center_left_choose_start_two_s").html(all);
			ChooseTime();
			CancelChoose();
		},
		error : function() {
			console.log("访问失败");
		},
		complete : function() {
			console.log("请求完成");
		},
	});
}